<?php
class App extends Module {
    
    public $router = array();
    
    public function run() {
        
        
        //$access = $this->access();
        $router = $this->get_router();
        $m = $router['m'];
        $a = $router['a'];
        
        $result = m($m)->$a();
        if ($result instanceof View) {
            $layout = $this->get_layout();
            $layout->add_view($result);
            return $layout;
        } else {
            return $result;
        }
        
        
        
        
        /*if (true === $access) {
            return m($m)->$a();
        } else if (false === $access){
            //echo 'cannot access';
            return new View_text('You have no permision to do this.');
        } else {
            return $access;
        }*/
        //$this->call($router['m'], $router['a']);
    }
    
    /*public function call($m, $a) {
        m($m)->$a();
    }*/
    
    public function access($m, $a) {
        $info = $this->model('router')->info($m, $a);
        if ($info && isset($info['access_mode']))
            $access_mode = $info['access_mode'];
        else {
            $access_mode = '';
        }
        
        if ($access_mode === ROUTER_ACCESS_PUBLIC) {
            return true;
        }
        $m_user = m('user');
        $login = $m_user->login();
        
        if (true === $login) {
            if ($access_mode === ROUTER_ACCESS_USER) {
                return true;
            }
            
            if ($m_user->is_admin()) {
                return true;
            }
            
            if ($access_mode === ROUTER_ACCESS_POSITION) {
                $router_id = $info['id'];
                $position_ids = $m_user->position_ids();
                
                var_dump($position_ids);
                
                if ($this->model('router_position')->contains($router_id, $position_ids)) {
                    return true;
                }
            }
            
            return false;
        } else {
            return $login;
        }
        
        return false;
    }
    
    
    private function get_router() {
        if ( ! $this->router) {
            $m = isset($_GET['m']) ? trim($_GET['m']) : tos_get_config('default_module');
            $a = isset($_GET['a']) ? trim($_GET['a']) : tos_get_config('default_action');
            $info = $this->model('router')->info($m, $a);
            
            if ($info && isset($info['access_mode']))
                $access_mode = $info['access_mode'];
            else {
                $access_mode = '';
            }
            
            if ($access_mode !== ROUTER_ACCESS_PUBLIC) {
                if (true !== m('user')->login()) {
                    $m = 'user';
                    $a = 'login';
                } else {
                    if ( ! m('user')->is_admin()) {
                        if ($access_mode === ROUTER_ACCESS_POSITION) {
                            $router_id = $info['id'];
                            $position_ids = m('user')->position_ids();
                            
                            if ( ! $this->model('router_position')->contains($router_id, $position_ids)) {
                                $m = 'app';
                                $a = 'nopermission';
                            }
                        }
                    }
                }
            }
            
            $this->router = array(
                'id' => $info['id'],
                'm'  => $m,
                'a'  => $a
            );
            /*$this->router = array(
                'id'            => $info['id'],
                'm'             => $m,
                'a'             => $a,
                'access_mode'   => $info['access_mode']
            );*/
        }
        
        return $this->router;
    }
    
    
    private function get_layout() {
        return $this->view('layout');
    }
}
?>